package com.frxuexi.shop2307.user

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.frxuexi.lib_base.alipay.MyPay
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.mvvm.BaseViewActivity
import com.frxuexi.shop2307.R
import com.frxuexi.shop2307.adapter.RechargeAdapter
import com.frxuexi.shop2307.databinding.ActivityMyMoneyBinding
import com.frxuexi.shop2307.shop.ShopViewModel
import dagger.hilt.android.AndroidEntryPoint
import org.greenrobot.eventbus.EventBus
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

@Route(path = "/app/MyMoneyActivity")
@AndroidEntryPoint
class MyMoneyActivity : BaseActivity<ActivityMyMoneyBinding,ShopViewModel>() {
    val rechargeAdapter by lazy { RechargeAdapter() }
    var sum=0f
    override fun initData() {
        vm.rechargeData.observe(this){
            rechargeAdapter.apply {
                data.clear()
                it.rechargeQuota.get(0).isCheck=true
                sum=it.rechargeQuota.get(0).price.toFloat()+it.rechargeQuota.get(0).giveMoney.toFloat()
                addData(it.rechargeQuota)
                notifyDataSetChanged()
            }
        }

    }

    override fun initView() {
        EventBus.getDefault().register(this)
        binding.myRv.apply {
            adapter=rechargeAdapter
            layoutManager=GridLayoutManager(this@MyMoneyActivity,3)
        }
        vm.recharge()
        binding.myConWei.setOnClickListener {
            binding.myConWei.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_stroke_green)
            binding.myConZhi.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_stroke_gray)
        }

        binding.myConZhi.setOnClickListener {
            binding.myConZhi.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_stroke_green)
            binding.myConWei.setBackgroundResource(com.frxuexi.lib_base.R.drawable.shape_stroke_gray)
        }

        rechargeAdapter.setOnItemClickListener { adapter, view, position ->
            rechargeAdapter.data.forEach {
                it.isCheck=false
            }
            val item = rechargeAdapter.data.get(position)
                item.isCheck=true
            rechargeAdapter.notifyDataSetChanged()
           sum=item.price.toFloat()+item.giveMoney.toFloat()
        }

        binding.myBtn.setOnClickListener {
            val item=rechargeAdapter.data.filter { it.isCheck }.get(0)
            MyPay.payV2(item.price,this)
        }

        SPUtils.getInstance().apply {
//                获取原来的总金额
            val m=getFloat("money",0.0f)
            binding.myMoney.setText("￥${m}")
        }
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    fun getPay(result:String){
        if(result=="payOk"){
            SPUtils.getInstance().apply {
//                获取原来的总金额
                val m=getFloat("money",0.0f)
                put("money",m+sum)
                binding.myMoney.setText("￥${m+sum}")
            }
        }
    }

}